package com.ant.storage.utils;


import cn.hutool.core.date.DateTime;

import java.text.SimpleDateFormat;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @author hzd
 * @version 1.0
 * @date 2022/3/29 10:31
 */
public class RandomUtil {

    private static final SimpleDateFormat dateFormatOne=new SimpleDateFormat("yyyyMMddHHmmssSS");

    private static final ThreadLocalRandom random=ThreadLocalRandom.current();
    //生成订单编号-方式一
    public static String generateOrderCode(){
        //TODO:时间戳+N为随机数流水号
        return dateFormatOne.format(DateTime.now().toJdkDate()) + generateNumber(3);
    }

    //N为随机数流水号
    public static String generateNumber(final int num){
        StringBuffer sb=new StringBuffer();
        for (int i=1;i<=num;i++){
            sb.append(random.nextInt(6));
        }
        return sb.toString();
    }

    /**
     * 生成订单号
     */
    public static String getOrderNumbers(String lastID) {
        String newNumber=""; //
        String yyyyMMdd = DateFormat.getDateFormat("yyyyMMdd");
        String substring="";
        if(lastID!=null){
        substring = lastID.substring(0, 8); //2022 0328 0004
        }
        if (yyyyMMdd.equals(substring)) {
            Integer countnumber = Integer.valueOf(lastID.substring(8)) + 1; //2022011880008

            if (countnumber.toString().length()==1) {
                newNumber = yyyyMMdd + "000" + countnumber.toString();
            }
            if (countnumber.toString().length()==2) {
                newNumber = yyyyMMdd + "00" + countnumber.toString();
            }
            if (countnumber.toString().length()==3) {
                newNumber = yyyyMMdd + "0" + countnumber.toString();
            }
            if (countnumber.toString().length()==4) {
                newNumber = yyyyMMdd + countnumber.toString();
            }
        } else {
            newNumber = yyyyMMdd + "0001";
        }
        return newNumber;
    }
}
